package org.openmore.consumer.controller.api;

import io.swagger.annotations.*;
import org.openmore.consumer.annotation.TokenAuthCheck;
import org.openmore.consumer.controller.common.BaseController;
import org.openmore.consumer.dto.common.BaseResponse;
import org.openmore.consumer.dto.common.Pagination;
import org.openmore.consumer.dto.common.ResourcesDto;
import org.openmore.consumer.dto.common.StringParams;
import org.openmore.consumer.entity.enums.ResourceType;
import org.openmore.consumer.service.feign.ResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

/**
 * THIS IS AUTO GENERATED SOURCE CODE
 * Created by org.openmore
 * on 2018-12-21
 */
@Api(value = "/Resources", tags = "", description = "资源库操作")
@RequestMapping(value = "/api/resources", produces = {APPLICATION_JSON_UTF8_VALUE})
@RestController
public class ResourcesController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ResourceService resourceService;

    @TokenAuthCheck
    @ApiOperation(value = "分页返回所有数据", response = ResourcesDto.class, responseContainer = "List")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到id={id}的", response = ResourcesDto.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    public BaseResponse selectAllByPage(@RequestParam(required = false) @ApiParam(value = "资源名称") String name,
                                        @RequestParam(required = false) @ApiParam(value = "资源存储key") String key,
                                        @ApiParam(value = "资源类型:IMAGE:图片、AUDIO：音频、VIDEO：视频、OTHERS:其它类型") ResourceType type,
                                        @RequestParam(required = false) @ApiParam(value = "开始时间") Date startTime,
                                        @RequestParam(required = false) @ApiParam(value = "结束时间") Date endTime,
                                        @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "分页第几页") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页多少记录") Integer pageSize) {
        logger.debug(">> selectAllByPage");
        return resourceService.selectAllByPage(name, key, type, startTime, endTime, pageNum, pageSize);
    }

    @TokenAuthCheck
    @ApiOperation(value = "根据id获取信息", response = ResourcesDto.class)
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：找不到id={id}的", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public BaseResponse getResourcesById(@PathVariable @ApiParam(required = true, value = "id") String id) {
        logger.debug(">> getResourcesById");
        return resourceService.getResourcesById(id);
    }

    @TokenAuthCheck
    @ApiOperation("更新")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：更新的数据不存在", response = BaseResponse.class),
            @ApiResponse(code = 400, message = "请求失败：数据更新失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.PUT)
    public BaseResponse updateResources(@RequestBody @ApiParam(value = "新信息", required = true) ResourcesDto resources) {
        logger.debug(">> updateResources");
        return resourceService.updateResources(resources);

    }

    @TokenAuthCheck
    @Transactional
    @ApiOperation("创建视频资源")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据创建失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.POST)
    public BaseResponse addVideoResources(@RequestBody @ApiParam(value = "新资源", required = true) ResourcesDto resources) {
        logger.debug(">> addVideoResources");
        return resourceService.addVideoResources(resources);

    }

    @TokenAuthCheck
    @ApiOperation("批量创建")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据创建失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/files")
    public BaseResponse insertResources(@RequestParam(value = "files") MultipartFile[] files) {
        logger.debug(">> insertResources");
        return resourceService.insertResources(files);
    }

    @TokenAuthCheck
    @Transactional
    @ApiOperation("单文件上传")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据创建失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/file")
    public BaseResponse insertResource(@RequestParam(value = "file") MultipartFile file) {
        logger.debug(">> insertResources");
        return resourceService.insertResource(file);
    }

//    @TokenAuthCheck
    @ApiOperation("获得直传Token")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据创建失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/uploadToken")
    public BaseResponse getUploadToken() {
        logger.debug(">> getUploadKey");
        return resourceService.getUploadToken();
    }

    @TokenAuthCheck
    @ApiOperation(value = "物理删除阿里云图片")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据删除失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/oss/physical")
    public BaseResponse deleteOssResource(@RequestBody @ApiParam(value = "oss对像名") StringParams fileName) {
        logger.debug(">> deleteResources");
        return resourceService.deleteOssResource(fileName);
    }


    @TokenAuthCheck
    @ApiOperation(value = "删除指定资源")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据删除失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public BaseResponse deleteResources(@PathVariable @ApiParam(value = "id", required = true) String id) {
        logger.debug(">> deleteResources");
        return resourceService.deleteResources(id);
    }

    @TokenAuthCheck
    @ApiOperation(value = "下载指定资源")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据删除失败", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{id}/download")
    public BaseResponse downloadResources(@PathVariable @ApiParam(value = "id", required = true) String id) {
        logger.debug(">> deleteResources");
        return resourceService.downloadResources(id);
    }

//    @TokenAuthCheck
    @ApiOperation("oss直传Token回调")
    @ApiResponses(value = {@ApiResponse(code = 400, message = "请求失败：数据创建失败", response = ResourcesDto.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/uploadCallback", produces = {"application/json;charset=UTF-8"}, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public BaseResponse uploadCallback(
            @RequestParam(required = false) @ApiParam(value = "文件名") String filename,
            @RequestParam(required = false) @ApiParam(value = "文件大小") String size,
            @RequestParam(required = false) @ApiParam(value = "文件类型") String mimeType,
            @RequestParam(required = false) @ApiParam(value = "高度") String height,
            @RequestParam(required = false) @ApiParam(value = "宽度") String width) {
        logger.debug(">> uploadCallback");
        return resourceService.uploadCallback(filename, size, mimeType, height, width);
    }

    @TokenAuthCheck
    @Transactional
    @ApiOperation(value = "导入保利数据", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/parseExcelPolvs", produces = {APPLICATION_JSON_UTF8_VALUE})
    public BaseResponse parseExcelProducts(@RequestParam() @ApiParam(value = "excel文件") MultipartFile file) {
        logger.debug(">> parseExcelProducts");
        return resourceService.parseExcelProducts(file);
    }

    //@TokenAuthCheck
    @ApiOperation(value = "下载保利数据导入模板", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/parsePolvExcelModel")
    public BaseResponse parseExcelModel() {
        logger.debug(">> parseExcelModel");
        return resourceService.parseExcelModel();
    }
    //@TokenAuthCheck
    @ApiOperation(value = "测试鉴权", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/testAuth")
    public BaseResponse test(@RequestParam(required = false) @ApiParam String str) {
        logger.debug(">> test");
        return resourceService.test(str);
    }
}




